<template>
  <div style="padding: 40px">
    <el-container>
      <el-row style="width: 100%">
        <el-col :span="5" v-for="index in worksList" :offset="1" pull="1" style="width: 290px">
          <div v-if="index.type === 3" class="card_div">
            <el-card :body-style="{ padding: '0px'}" >
              <div>
                <div class="card-top">
                  <el-image :src=index.url class="image" fit="fill"></el-image>
                </div>
                <div class="message">
                  <div class="bottom">
                    <time class="type">照片</time>
                    <el-button type="text" class="button" @click="openViewer(index.url)">查看照片</el-button>
                    <el-image-viewer
                      v-if="showViewer"
                      :on-close="closeViewer"
                      :url-list="[url]" />
                  </div>
                </div>
              </div>
            </el-card>
          </div>
          <div v-else-if="index.type === 2" class="card_div">
            <el-card :body-style="{ padding: '0px' }" >
              <div>
                <div class="card-top">
                  <el-image :src="require('../../../assets/work_pic/video_pic.jpg')" fit="fill" class="image"></el-image>
                </div>
                <div class="message">
                  <div class="bottom ">
                    <time class="type">视频</time>
                    <el-button type="text" class="button" @click="videoPlay">播放视频</el-button>
                    <el-dialog title="视频播放" :visible.sync="dialogVideoPlayer" top="30px" width="70%" style="height: 570px">
                      <video :src=index.url controls class="video" :ref="dialogVideoPlayer"></video>
                    </el-dialog>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
          <div v-else-if="index.type === 1" class="card_div">
            <el-card :body-style="{ padding: '0px' }" >
              <div>
                <div class="card-top">
                  <el-image :src="require('../../../assets/work_pic/music_pic.jpg')" fit="fill" class="image" Opacity></el-image>
                </div>
                <div class="message">
                  <div class="bottom">
                    <time class="type">音频</time>
                    <el-popover
                      style="padding-left: 10px"
                      placement="bottom"
                      title="播放"
                      width="400px"
                      trigger="click">
                      <audio :src=index.url controls="controls"></audio>
                      <el-button type="text" class="button" slot="reference">播放音频</el-button>
                    </el-popover>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </el-container>
    <el-pagination
      layout="prev, pager, next"
      hide-on-single-page="true"
      :current-page="page.pageNum"
      :page-size="page.pageSize"
      :total="page.totalSize"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
import { getWorkLimt } from '../../../api/works'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: 'Works',
  components: { ElImageViewer },
  data() {
    return {
      baseUrl: this.global.picBaseUrl,
      dialogVideoPlayer: false,
      music_pic: require('../../../assets/work_pic/music_pic.jpg'),
      showViewer: false, // 显示查看器
      url: '',
      worksList: [],
      page: {
        pageNum: 1,
        pageSize: 12,
        totalPage: 0,
        totalSize: 0
      }
    }
  },
  mounted() {
    this.$refs.btn.onclik(function() {
      if (this.$refs.dialogVideo.paused) {
        this.$refs.dialogVideo.play()
      } else {
        this.$refs.dialogVideo.pause()
      }
    });
  },
  created() {
    this.getWorkList()
  },
  methods: {
    // 图片预览
    openViewer(url){
      this.url = url
      this.showViewer = true
    },
    // 关闭预览
    closeViewer() {
      this.showViewer = false
    },
    // 视频播放
    videoPlay(){
      this.dialogVideoPlayer = true
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getWorkList()
    },
    // 获取作品列表
    getWorkList() {
      let i = 0
      getWorkLimt({
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize
      }).then(res => {
        this.worksList = res.data.list
        this.page = res.data.page
        for (i = 0; i < this.worksList.length; i++) {
          this.worksList[i].url = this.baseUrl + this.worksList[i].url
          // this.worksList[i].url = 'http://192.168.43.101' + this.worksList[i].url
        }
        console.log('list', this.worksList)
      }).catch(err => {
        console.log('err', err)
      })
    }
  }
}
</script>

<style scoped>
  .card_div{
    height: 290px;
  }
  .type {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
    float: bottom;
  }
  .message {
    padding: 14px;
    margin-bottom: 2px;
}
  .button {
    padding: 0;
    float: right;
  }

  .image {
    height: 150px;
    display: block;
    width: 290px;
  }
  .video {
    display: block;
    margin: 0 auto;
    height: 400px;
  }
  .card-top {
    height: 150px;
    width: 290px;
  }
</style>
